package com.example.demo.constant;

import com.example.demo.vo.BaseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常拦截器
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler({BaseException.class})
    public BaseResult<String> baseException(BaseException ex) {
        String msg = ex.getMessage();
        ResultValueEnum resultValueEnum = (ResultValueEnum) ex.getBizExceptionEnum();
        if (resultValueEnum == null) {
            resultValueEnum = ResultValueEnum.SYS_FAILURE;
        }
        BaseResult<String> result = BaseResult.newInstance(resultValueEnum);
        if (msg != null) {
            result.setMsg(msg);
        }
        return result;
    }
}
